package com.hsnn.medstgmini.pro.base.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.pro.base.model.ViewBakpurchase;
import com.hsnn.medstgmini.pro.base.service.BaseBakpurchaseManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.model.UserType;
import com.hsnn.medstgmini.util.Pagination;
/**
 * @category 医疗机构备案CONTROLLER
 * @author 韩守松
 * @date 2015年6月23日
 */
@Controller
@RequestMapping("/baseBakpurchaseYY")
public class BaseBakpurchaseYYController {
	
	private static final Logger log = Logger.getLogger(BaseBakpurchaseController.class);
	
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private BaseBakpurchaseManager baseBakpurchaseManager;
	
	@RequestMapping(value="/toListJsp")
	public String toListJsp(HttpServletRequest request){
		SysUser sysUser = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		HospInfo hospInfo = sysUser.getInfo();
		String bakHospId = hospInfo.getHospId();
		request.setAttribute("bakHospId", bakHospId);
		request.setAttribute("name", sysUser.getName());
		return "/pro/bak/bakDrugList";
	}
	
	/**
	 * 备案审批查询
	 * 
	 * 用来封装json
	 * 
	 * @param request
	 * @param result
	 * @return
	 */
	@RequestMapping("/getDataList")
	@ResponseBody
	public Pagination getDataList(ViewBakpurchase bak) {
		Pagination page = new Pagination(request);
		try {
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			//	医疗机构只查看自己备案的，中心查看所有
			if (user.getType() == UserType.yy.getKey()) {
				HospInfo hospInfo = user.getInfo();
				bak.setHospCode(hospInfo.getHospCode());
			}
			page.setForm(bak);
			baseBakpurchaseManager.getBakPurchaseList(page, user);
			page.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error error) {
			error.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 备案药品详情
	 * @date 2015年11月20日
	 * @return
	 */
	@RequestMapping(value="/toDataDetailJsp")
	public String toDataDetailJsp(Model model, String bakPurId) {
		Map<String, Object> detail = baseBakpurchaseManager.getBakDetailById(bakPurId);
		model.addAttribute("detail", detail);
		return "/pro/bak/bakDrugSearchDetail";
	}

}